//
//  XNStatusCell.swift
//  weibo
//
//  Created by 王向宁 on 16/5/24.
//  Copyright © 2016年 王向宁. All rights reserved.
//微博首页自定义cell

import UIKit
import SnapKit

class XNStatusCell: UITableViewCell {
    
    var toolBarTopCons: Constraint?
    
    var statusViewModel : XNStatusViewModel? {
        didSet {
            statusOriginaview.statusViewModel = statusViewModel
            //判断是否是否是原创微博
            //卸载工具条的顶部约束对象
            toolBarTopCons?.uninstall()
            
            if statusViewModel?.status?.retweeted_status == nil {
                //原创微博
                //设置数据
                statusRetweetedView.statusViewModel = nil
                //隐藏转发微博
                statusRetweetedView.hidden = true
                //更新约束,底部工具条的顶部应该相对于原创微博底部
                statusToolBar.snp_updateConstraints(closure: { (make) in
                    self.toolBarTopCons =  make.top.equalTo(statusOriginaview.snp_bottom).constraint
                    
                })
            } else  {
                statusRetweetedView.statusViewModel = statusViewModel
                
                statusRetweetedView.hidden = false
                
                statusToolBar.snp_updateConstraints(closure: { (make) in
                    self.toolBarTopCons = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
                })
            }
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = randomColor()
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
        
        contentView.addSubview(statusOriginaview)
        
        contentView.addSubview(statusRetweetedView)
        
        contentView.addSubview(statusToolBar)
        
        
        //设置约束
        statusOriginaview.snp_makeConstraints { (make) in
            make.top.right.left.equalTo(self.contentView)
        }
        
        statusRetweetedView.snp_makeConstraints { (make) in
            make.top.equalTo(statusOriginaview.snp_bottom)
            
            make.left.right.equalTo(contentView)
        }
        
        statusToolBar.snp_makeConstraints { (make) in
            self.toolBarTopCons = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
            
            make.right.left.equalTo(self.contentView)
            make.height.equalTo(36)
        }
        
        contentView.snp_makeConstraints { (make) in
            make.bottom.equalTo(statusToolBar.snp_bottom)
            
            make.top.left.right.equalTo(self)
        }

    }
    
    
    //懒加载
    private lazy var statusOriginaview: XNStatusOriginalview = XNStatusOriginalview()
    
    private lazy var statusRetweetedView: XNStatusRetweetedView = XNStatusRetweetedView()
    
    private lazy var statusToolBar: XNStatusToolBar = XNStatusToolBar()

}
